package cn.com.utils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

public class Dateutils {
    public static int calculateDaysDifference(String date1, String date2) {
        // 创建DateTimeFormatter对象，用于解析和格式化日期字符串
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        // 将日期字符串解析为LocalDate对象
        LocalDate date1Obj = LocalDate.parse(date1, formatter);
        LocalDate date2Obj = LocalDate.parse(date2, formatter);
        // 计算两个日期之间的天数差
        long daysDifference = ChronoUnit.DAYS.between(date1Obj, date2Obj);
        // 返回天数差
        return (int) daysDifference;
    }

    public static String getNowDate(){
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        // 创建一个DateTimeFormatter格式化器，设置为所需的日期格式
        DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        // 将当前日期格式化为字符串
        String formattedDate = currentDate.format(dateFormatter);
        return formattedDate;
    }

}
